<?php


class chatlogApp extends BackendApp {
    

    var $_chatlog_mod;

    function __construct() {
        $this->Chat_logApp();
    }

    function Chat_logApp() {
        parent::BackendApp();
        $this->_chatlog_mod = & m('chatlog');
    }

    function index() {
        $conditions = '';
          // 处理分页
        $page = $this->_get_page(20);
        $coupon_codes_list = $this->_chatlog_mod->find([
            'fields' => 'chatlog.*,member.*',
            'conditions' => "chatlog.senderid = 0 " . $conditions,
            'join' => 'belongs_to_member',
            'order' => "sendtime desc",
            'limit' => $page['limit'],
            'count' => true,
        ]);
        $page['item_count'] = $this->_chatlog_mod->getCount();
        $this->_format_page($page);
        $this->assign('filtered', $conditions ? 1 : 0); //是否有查询条件
        $this->assign('page_info', $page);
        $this->assign('coupon_list', $coupon_codes_list);
        // 引入前端资源
        $this->import_resource(['script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
            'style' => 'jquery.ui/themes/ui-lightness/jquery.ui.css']);
        // 显示模板
        $this->display('chatlog.index.html');
    }

    //新增
    function add(){
        if(IS_POST){
            $postData = $_POST;
            if(!$postData['sendcontent']){
                $this->show_warning('请填写要发送的内容！');
                return;
            }
            if(!$postData['user_id']){
                $this->show_warning('请选择发送人！');
                return;
            }
            $user_id = $postData['user_id'];
            $user_ids = explode(',',$user_id);
            $time = time();

            $sql = "INSERT INTO ecm_chatlog (senderid,receiverid,sendtime,sendcontent,is_new,is_read) VALUES ";
            $count = count($user_ids);
            foreach($user_ids as $k=>$v){
                if($count-1 == $k){
                    $sql .= "(0,$v,$time,'".$postData['sendcontent']."',0,1)";
                }else{
                    $sql .= "(0,$v,$time,'".$postData['sendcontent']."',0,1),";
                }
            }
             if (!$article_id = $this->_chatlog_mod->db->query($sql)) {  
                $this->show_warning('新增失败！');
                return;
            } else {
                $this->admin_log("新增站内信[" . $postData['sendcontent'] . "]", 1);
            }
            $this->show_message('新增成功', 'back_list', 'index.php?app=chatlog', 'continue_add', 'index.php?app=chatlog&amp;act=add'
            );
        }else{
            $this->display('chatlog.form.html');
        }
    }
}